//
//  MyMeetingViewModel.swift
//  CleanArchitecture
//
//  Created by siri on 2024/11/6.
//  Copyright © 2024 Tuan Truong. All rights reserved.
//



import Combine
import UIKit
import Factory
//, ShowProductDetail
class MyMeetingRoomViewModel: GettingMyMeetingRoom{

    // swiftlint:disable:this final_class
    unowned let navigationController: UINavigationController
    
    @Injected(\.myMeetingRoomGateway)
    var myMeetingRoomGateway: MyMeetingRoomGatewayProtocol
    
    init(navigationController:
         UINavigationController) {
        self.navigationController = navigationController
    }
    
    // 跳转
//    func vm_showLoginDetail() {
//        showLogin()
//    }
//    
//    func vm_getProducts() -> AnyPublisher<[Product], Error> {
//        getProducts()
//    }
    func vm_getMyMeeting() -> AnyPublisher<[MyMeetingRoom], Error> {
        getMyMeetingRoom()
    }
    deinit {
        print("MyMeetingViewModel deinit")
    }
}

// MARK: - ViewModel
extension MyMeetingRoomViewModel: ObservableObject, ViewModel {
    struct Input {
        let loadTrigger: AnyPublisher<Void, Never>
        let reloadTrigger: AnyPublisher<Void, Never>
        let selectTrigger: AnyPublisher<Void, Never>
    }
    
    final class Output: ObservableObject {
        @Published var meetings = [MyMeetingRoom]()
        @Published var isLoading = false
        @Published var isReloading = false
        @Published var alert = AlertMessage()
    }
    
    func transform(_ input: Input, cancelBag: CancelBag) -> Output {
        let config = ListFetchConfig(initialLoadTrigger: input.loadTrigger,
                                     reloadTrigger: input.reloadTrigger,
                                     fetchItems: self.vm_getMyMeeting)
        
        let (meetings, error, isLoading, isReloading) = fetchList(config: config).destructured
        
        let output = Output()
        
        meetings
            .map { $0 }
            .assign(to: \.meetings, on: output)
            .store(in: cancelBag)
        
        error
            .receive(on: RunLoop.main)
            .map { AlertMessage(error: $0) }
            .assign(to: \.alert, on: output)
            .store(in: cancelBag)
        
        isLoading
            .assign(to: \.isLoading, on: output)
            .store(in: cancelBag)
        
        isReloading
            .assign(to: \.isReloading, on: output)
            .store(in: cancelBag)
        
        input.selectTrigger
            .sink(receiveValue: { indexPath in
//                let product = output.products[indexPath.row].product
//                self.vm_showProductDetail(product: product)
//                self.vm_showLoginDetail()
            })
            .store(in: cancelBag)
        
        return output
    }
}


